<?php
 if(!defined('IN_CAI')) {
	exit('Access Denied');
}
/**
 * 图片管理
 *
 */
$ac = $_REQUEST['ac'];
$url = '/admin/index.php?do=image';
if (empty($ac)) {
	$ac = 'list';
}
if ($ac == 'list') {
	$page = intval($_GET['page']) >0 ? intval($_GET['page']) : 1;
	$limit = 20;
	$offset = ($page - 1) *$limit;
	$keyword = trim ($_GET['keyword']);
	$classid = intval($_GET['classid']);
	$where = " where 1 ";
	if (!empty($keyword)) {
		$where .= " and title like '%$keyword%'";
	}

	if (!empty($classid)) {
		$where .= " and classid = $classid";
	}
	$count = $db->result($db->query("select count(*) from ".tname('image')." $where"),0);
	$query = $db->query("select * from ".tname('image')." $where order by id desc limit $offset,$limit");
	$list = $db->fetch_all($query);
	$pagestr = multi($count,$limit,$page,$url);
	//classlist
	$query = $db->query("select * from ".tname("class")." order by classid desc");
	$classlist = $db->fetch_all($query);
	$options = '';
	foreach ($classlist as $val) {
		$options .="<option value='{$val['classid']}' ".($classid == $val['classid'] ?'selected="selected"':'').">{$val['classname']}</option>";
	}
	include atemplate('do_image_list');
} elseif ($ac == 'add' || $ac == 'edit') {

	if (empty($_POST['step'])) {
		$id = intval($_GET['id']);
		if ($ac == 'edit' && $id > 0) {
			$query = $db->query("select * from ".tname('image')." where id = $id");
			$info = $db->fetch_array ($query);
			if (empty($info)) {
				adminmessage('对不起，找不到改信息');
			}
		}
		//classlist
		$query = $db->query("select * from ".tname("class")." order by classid desc");
		$classlist = $db->fetch_all($query);
		$options = '';
		foreach ($classlist as $val) {
			$options .="<option value='{$val['classid']}' ".($info['classid'] == $val['classid'] ?'selected="selected"':'').">{$val['classname']}</option>";
		}
		if ($info['url']) {
			$image = "<img src='{$info['url']}' width=120 height=100 /><br />";
		}
		include atemplate('do_image_add');
	}else {
		$id = intval($_POST['id']);
		$classid = trim($_POST['classid']);
		$title = trim($_POST['title']);
		$data['title'] = $title;
		$data['classid'] = $classid;
		$data['createtime'] = time();
		if (empty($data['classid'])) {
			adminmessage('请选择分类');
		}
		if (empty($data['title'])) {
			adminmessage('请填写标题');
		}
		$fileurl = $_FILES['url'];
		if ($id && !$fileurl['name']) {
			$fileurl = array();
		}
		if (!id && empty($fileurl['name'])) {
			adminmessage('请上传图片');
		}
		if (!empty($fileurl)) {
			if ($fileurl['error'] !== 0) {
				adminmessage('上传图片发生错误');
			}else {
				$extname = fileext($fileurl['name']);
				$allow = array('jpg','jpeg','gif','png','tif');
				if(!in_array($extname,$allow)) {
					adminmessage('请上传以'.join(',',$allow)."结尾的图片");
				}
				$uploadurl = 'upload/img/'.md5(time()).'.'.$extname;
				move_uploaded_file($fileurl['tmp_name'],S_ROOT.$uploadurl);
				$data['url'] = '/'.$uploadurl;
			}
		}

		if (!$id) {
			inserttable('image',$data);
			adminmessage('添加图片成功',$url);
		}else{
			updatetable('image',$data,array('id'=>$id));
			adminmessage('修改图片成功',$url);
		}
	}
} elseif ($ac == 'del') {
	$id = intval($_GET['id']);
	if (!id) {
		adminmessage('非法ID');
	} else {
		$db->query('delete from '.tname('image').' where id='.$id);
		if ($db->affected_rows()) {
			adminmessage('删除图片成功',$url);
		}else {
			adminmessage('删除图片失败',$url);
		}
	}
} elseif ($ac == 'class') {
	//添加分类
	$classname = trim($_POST['classname']);
	if (empty($classname)) {
		adminmessage('请输入分类名称');
	}
	$count = getcount('class',array('classname'=>$classname));
	if ($count >0 ){
		adminmessage('该分类已经存在');
	}
	inserttable('class',array('classname'=>$classname));
	adminmessage('添加分类成功',$url);
}
?>